package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import com.hngates.project.domain.IdName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.ConstructionUnitMapper;
import com.hngates.project.domain.ConstructionUnit;
import com.hngates.project.service.IConstructionUnitService;
import com.hngates.common.core.text.Convert;

/**
 * 单位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-24
 */
@Service
public class ConstructionUnitServiceImpl implements IConstructionUnitService {
    @Autowired
    private ConstructionUnitMapper constructionUnitMapper;

    /**
     * 查询单位信息
     *
     * @param id 单位信息主键
     * @return 单位信息
     */
    @Override
    public ConstructionUnit selectConstructionUnitById(Long id) {
        return constructionUnitMapper.selectConstructionUnitById(id);
    }

    /**
     * 查询单位信息列表
     *
     * @param constructionUnit 单位信息
     * @return 单位信息
     */
    @Override
    public List<ConstructionUnit> selectConstructionUnitList(ConstructionUnit constructionUnit) {
        return constructionUnitMapper.selectConstructionUnitList(constructionUnit);
    }

    /**
     * 新增单位信息
     *
     * @param constructionUnit 单位信息
     * @return 结果
     */
    @Override
    public int insertConstructionUnit(ConstructionUnit constructionUnit) {
        constructionUnit.setCreateTime(DateUtils.getNowDate());
        return constructionUnitMapper.insertConstructionUnit(constructionUnit);
    }

    /**
     * 修改单位信息
     *
     * @param constructionUnit 单位信息
     * @return 结果
     */
    @Override
    public int updateConstructionUnit(ConstructionUnit constructionUnit) {
        constructionUnit.setUpdateTime(DateUtils.getNowDate());
        return constructionUnitMapper.updateConstructionUnit(constructionUnit);
    }

    /**
     * 批量删除单位信息
     *
     * @param ids 需要删除的单位信息主键
     * @return 结果
     */
    @Override
    public int deleteConstructionUnitByIds(String ids) {
        return constructionUnitMapper.deleteConstructionUnitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单位信息信息
     *
     * @param id 单位信息主键
     * @return 结果
     */
    @Override
    public int deleteConstructionUnitById(Long id) {
        return constructionUnitMapper.deleteConstructionUnitById(id);
    }

    @Override
    public List<IdName> selectunitId() {

        return constructionUnitMapper.selectunitId();
    }

    /**
     * @param constructionUnitList 建设单位列表信息
     * @param updateSupport        是否支持更新 如果已经存在则进行更新数据
     * @param operName             操作用户
     * @return
     */

    @Override
    public String importConstructionUnit(List<ConstructionUnit> constructionUnitList, Boolean updateSupport, String operName) {
        if (Objects.isNull(constructionUnitList) || constructionUnitList.size() == 0) {
            throw new BusinessException("导入单位信息不能为空");
        }

        StringBuilder importMsg = new StringBuilder();
        //循环Excel中的记录
        for (ConstructionUnit constructionUnit : constructionUnitList) {

            try {
                //constructionUnit是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                ConstructionUnit constructionUnit1 = constructionUnitMapper.selectConstructionUnitByUnitId(constructionUnit.getUnitId());
                //判断数据库对象是否存在
                if (constructionUnit1 == null) {
                    //如果不存在就创建
                    constructionUnit1 = new ConstructionUnit();
                    //给对象得字段名赋值
                    constructionUnit1.setUnitId(constructionUnit.getUnitId());
                    constructionUnit1.setLeader(constructionUnit.getLeader());
                    constructionUnit1.setPhone(constructionUnit.getPhone());
                    constructionUnit1.setLgMoney(constructionUnit.getLgMoney());
                    constructionUnit1.setLgBalance(constructionUnit.getLgBalance());
                    constructionUnit1.setLgDate(constructionUnit.getLgDate());

                    //赋值完成后 将这个对象插入到数据库中
                    constructionUnitMapper.insertConstructionUnit(constructionUnit1);

                    importMsg.append("<br/>" + "单位信息" + constructionUnit1.getUnitId() + "导入成功");

                } else if (updateSupport) {
                    //更新
                    constructionUnit1.setUpdateBy(operName);
                    constructionUnit1.setLeader(constructionUnit.getLeader());
                    constructionUnit1.setPhone(constructionUnit.getPhone());
                    constructionUnit1.setLgMoney(constructionUnit.getLgMoney());
                    constructionUnit1.setLgBalance(constructionUnit.getLgBalance());
                    constructionUnit1.setLgDate(constructionUnit.getLgDate());

                    constructionUnitMapper.updateConstructionUnit(constructionUnit1);
                    importMsg.append("<br/>" + "单位信息" + constructionUnit1.getUnitId() + "更新成功");

                } else {
                    importMsg.append("<br/>" + "单位信息" + constructionUnit1.getUnitId() + "已存在");
                }

            } catch (Exception e) {
                importMsg.append("<br/>" + "单位信息" + constructionUnit.getUnitId() + "导入时异常");
            }
        }
        return importMsg.toString();
    }
}
